package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SystemDivine;
import com.ruoyi.system.service.ISystemDivineService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统占卜配置Controller
 * 
 * @author DEREK
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/system/divine")
public class SystemDivineController extends BaseController
{
    @Autowired
    private ISystemDivineService systemDivineService;

    /**
     * 查询系统占卜配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:divine:list')")
    @GetMapping("/list")
    public TableDataInfo list(SystemDivine systemDivine)
    {
        startPage();
        List<SystemDivine> list = systemDivineService.selectSystemDivineList(systemDivine);
        return getDataTable(list);
    }

    /**
     * 导出系统占卜配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:divine:export')")
    @Log(title = "系统占卜配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemDivine systemDivine)
    {
        List<SystemDivine> list = systemDivineService.selectSystemDivineList(systemDivine);
        ExcelUtil<SystemDivine> util = new ExcelUtil<SystemDivine>(SystemDivine.class);
        util.exportExcel(response, list, "系统占卜配置数据");
    }

    /**
     * 获取系统占卜配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:divine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(systemDivineService.selectSystemDivineById(id));
    }

    /**
     * 新增系统占卜配置
     */
    @PreAuthorize("@ss.hasPermi('system:divine:add')")
    @Log(title = "系统占卜配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SystemDivine systemDivine)
    {
    	if(StringUtils.isEmpty(systemDivine.getExtend2())) {
    		return AjaxResult.error("请输入code编码");
    	}
    	int count = systemDivineService.count(new LambdaQueryWrapper<SystemDivine>().eq(SystemDivine::getType, systemDivine.getType()).eq(SystemDivine::getExtend2, systemDivine.getExtend2()));
    	if(count > 0) {
    		return AjaxResult.error("code已经存在");
    	}
        return toAjax(systemDivineService.insertSystemDivine(systemDivine));
    }

    /**
     * 修改系统占卜配置
     */
    @PreAuthorize("@ss.hasPermi('system:divine:edit')")
    @Log(title = "系统占卜配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemDivine systemDivine)
    {
        return toAjax(systemDivineService.updateSystemDivine(systemDivine));
    }

    /**
     * 删除系统占卜配置
     */
    @PreAuthorize("@ss.hasPermi('system:divine:remove')")
    @Log(title = "系统占卜配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(systemDivineService.deleteSystemDivineByIds(ids));
    }
}
